package com.anrongtec.zcpt.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.base.CommonSeletorAdapter;
import com.anrongtec.zcpt.module.Type;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/**
 * 搜索标签类的View
 * 
 * @ClassName: SearchTagView
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author dongtianhao
 * @param <T>
 * @date 2017年3月17日 上午11:25:14
 *
 */
public class SearchTagView extends LinearLayout  {
	private Context context;
	private List<List<Type>> dataSource;
	private SparseArrayCompat<RecyclerView> mSarseArray;
	private List<CommonSeletorAdapter<Type>> adapterList;
	private HashMap<String, Type> mSearchTermsMap;

	public SearchTagView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		this.context = context;
		setOrientation(LinearLayout.VERTICAL);
		mSearchTermsMap = new HashMap<String, Type>();
	}

	public SearchTagView(Context context, AttributeSet attrs, int defStyleAttr) {
		this(context, attrs, defStyleAttr, 0);
	}

	public SearchTagView(Context context, AttributeSet attrs) {
		this(context, attrs, 0, 0);
	}

	public SearchTagView(Context context) {
		this(context, null);
	}

	public void setSourceData(List<List<Type>> list) {
		this.dataSource = list;
		if (null == list) {
			return;
		}
		mSarseArray = new SparseArrayCompat<RecyclerView>();
		adapterList = new ArrayList<CommonSeletorAdapter<Type>>();
		initReclcleView();
	}

	private void initReclcleView() {
		for (int i = 0; i < dataSource.size(); i++) {
			final CommonSeletorAdapter<Type> mAdapter = new CommonSeletorAdapter<Type>(context,
					R.layout.item_search_tag_label, dataSource.get(i), 0) {

				@Override
				public void convertItem(ViewHolder holder, Type item, int position) {
					if (item.getValue().equals(item.getParentValue())) {
						holder.setText(R.id.tv_item_label, item.getName() + ":");
					} else {
						holder.setText(R.id.tv_item_label, item.getName());
					}
					holder.setTextSytle(R.id.tv_item_label, Typeface.NORMAL);
					holder.setTextColorRes(R.id.tv_item_label, R.color.colorPrimary_blue);
					holder.setBackgroundRes(R.id.tv_item_label, R.color.white);
				}

				@Override
				public void selectItem(ViewHolder holder, Type item, int selection, boolean isDefault) {
					if (item.getValue().equals(item.getParentValue())) {
						holder.setText(R.id.tv_item_label, item.getName() + ":");
						mSearchTermsMap.remove(item.getValue());
					} else {
						holder.setText(R.id.tv_item_label, item.getName());
						mSearchTermsMap.put(item.getParentValue(), item);
					}
					holder.setTextSytle(R.id.tv_item_label, Typeface.BOLD);
					holder.setBackgroundRes(R.id.tv_item_label, R.color.colorPrimary_blue);
					holder.setTextColorRes(R.id.tv_item_label, R.color.white);
					if (null != onSeclctListener) {
						onSeclctListener.onSelect(getSearchList());
					}
				}

			};
			adapterList.add(mAdapter);
		}
		for (int i = 0; i < dataSource.size(); i++) {
			RecyclerView recyclerView = new RecyclerView(context);
			recyclerView.setPadding(10, 10, 0, 10);
			recyclerView.setId(i);
			// 设置布局管理器,横向
			LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
			linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
			recyclerView.setLayoutManager(linearLayoutManager);
			mSarseArray.put(i, recyclerView);
			recyclerView.setAdapter(adapterList.get(i));
			addView(recyclerView);
		}
	}

	public List<Type> getSearchList() {
		ArrayList<Type> l = new ArrayList<Type>();
		if (mSearchTermsMap.isEmpty()) {
			return l;
		}
		for (Map.Entry<String, Type> entry : mSearchTermsMap.entrySet()) {
			Type t =entry.getValue();
			l.add(t);
		}
		return l;
	}

	public void setOnselectListener(onSeclctListener onSeclctListener) {
		this.onSeclctListener = onSeclctListener;
	}

	public interface onSeclctListener {
		void onSelect(List<Type> mTypes);
	};

	private onSeclctListener onSeclctListener;



}
